import axios from 'axios';
import config from './config';
import router from '@/router'
import Vue from 'vue'
import Message from 'muse-ui-message'
import 'muse-ui-message/dist/muse-ui-message.css';
import Toast from 'muse-ui-toast';

// 使用vuex做全局loading时使用
// import store from '@/store'

function $axios (options) {
    var that = this;
    return new Promise((resolve, reject) => {
        const instance = axios.create({
            baseURL: config.baseURL,
            headers: {},
        })

        // request 拦截器
        instance.interceptors.request.use( config => {
            return config
        }, error => {
            // 请求错误时
            console.log('request:', error)
            // 1. 判断请求超时
            if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
                console.log('timeout请求超时')
                // return service.request(originalRequest);//再重复请求一次
            }
            // 2. 需要重定向到错误页面
            const errorInfo = error.response

            if (errorInfo) {
            //error =errorInfo.data  //页面那边catch的时候就能拿到详细的错误信息,看最下边的Promise.reject
                const errorStatus = errorInfo.status; // 404 403 500 ...
                router.push({
                    path: `/error/${errorStatus}`
                })
            };
            return Promise.reject(error) // 在调用的那边可以拿到(catch)你想返回的错误信息
        })

        // response 拦截器
        instance.interceptors.response.use(response => {
            let data;
            if (response.data == undefined) {
                data = JSON.parse(response.request.responseText)
            } else {
                data = response.data.tdata
            }
            switch (response.data.code) {
                case 'v':
                    return response.data.tdata;
                break;
                case 'page':
                    return response.data;
                break;
                case 'login-index':
                    Message.alert(response.data.msg + ',请重新登录。', '提示', {
                        type: 'warning'
                    }).then(() => {
                        window.location.assign(window.location.origin + window.location.pathname)
                    })
                break;

                case 'valerror':
                    Message.alert(response.data.msg,'提示', {
                        type: 'error'
                    })
                break;
                case 'error':
                    Message.alert(response.data.msg,'提示', {
                        type: 'error'
                    })
                    // return response.data;
                    return Promise.reject(response.data);   
                break;
                default:
            }

            // 若不是正确的返回code，且已经登录，就抛出错误
            // const err = new Error(data.desc)
            // err.data = data
            // err.response = response
            // throw err
            return response.data.tdata;
        }, err => {
            if (err && err.response) {
                switch (err.response.status) {
                    case 400:
                        err.message = '请求错误'
                        break

                    case 401:
                        err.message = '未授权，请登录'
                        break

                    case 403:
                        err.message = '拒绝访问'
                        break

                    case 404:
                        err.message = `请求地址出错: ${err.response.config.url}`
                        break

                    case 408:
                        err.message = '请求超时'
                        break

                    case 500:
                        err.message = '服务器内部错误'
                        break

                    case 501:
                        err.message = '服务未实现'
                        break

                    case 502:
                        err.message = '网关错误'
                        break

                    case 503:
                        err.message = '服务不可用'
                        break

                    case 504:
                        err.message = '网关超时'
                        break

                    case 505:
                        err.message = 'HTTP版本不受支持'
                        break

                    default:
                }
            }
            console.error(err)
            return Promise.reject(err) // 返回接口返回的错误信息
        });

        // 请求处理
        instance(options).then(res => {
            resolve(res)
            return false
        }).catch(error => {
            reject(error)
        })
    })
}

Vue.prototype.$axios = $axios;

export default $axios
